package com.wm.ad_navi_new.skin

import android.content.res.XmlResourceParser
import android.util.Xml
import android.view.View
import com.wm.ad_navi_new.MyApplication
import com.wm.ad_navi_new.SkinConfig
import com.wm.ad_navi_new.skin.util.AttrUtil
import com.wm.ad_navi_new.skin.util.SkinListUtil
import org.xmlpull.v1.XmlPullParser

object ParseAttrUtil {

    fun xmlLayoutParser(rootView: View, layoutXmlId: Int) : Map<View, SkinItem> {

        var mSkinItemMap = HashMap<View, SkinItem>()

        // 如果布局ID没有的情况，直接返回
        if (layoutXmlId == 0) {
            return  mSkinItemMap
        }

        val parser: XmlResourceParser?

        try {
            // 根据资源ID，生成1个XmlResourceParser
            parser = MyApplication.context.resources.getLayout(layoutXmlId)
            val attributeSet = Xml.asAttributeSet(parser)

            var type: Int
            while (parser.next().also { type = it } != XmlPullParser.END_DOCUMENT) {
                if (type != XmlPullParser.START_TAG) {
                    continue
                }

                val viewAttrs = mutableListOf<AbstractSkinAttr>()

                // 是否支持换肤
                val isEnable: Boolean = attributeSet.getAttributeBooleanValue(SkinConfig.NAMESPACE,SkinConfig.ATTR_SKIN_ENABLE,false)

                if (isEnable) {
                    var viewId = 0
                    for (i in 0 until attributeSet.attributeCount) {
                        val attrName = attributeSet.getAttributeName(i)  // background
                        val attrValue = attributeSet.getAttributeValue(i) // "@2131034145"

                        // 解析Id

                        // 解析Id
                        if ("id" == attrName) {
                            viewId = attrValue.substring(1).toInt()  // id: 2131034145
                        }

                        //endregion
                        //if attrValue is reference，eg:@color/red
                        if (AttrUtil.isSupportedAttr(attrName) && attrValue.startsWith("@")) {
                            try {
                                //resource id
                                val id = attrValue.substring(1).toInt() // id: 2131034145
                                if (id == 0) {
                                    continue
                                }
                                val entryName: String = MyApplication.context.resources.getResourceEntryName(id) // entryName: "black"
                                val typeName: String = MyApplication.context.resources.getResourceTypeName(id)  // typeName: "coLor"
                                val mSkinAttr: AbstractSkinAttr? = AttrUtil.get(attrName, id, entryName, typeName) //
                                if (mSkinAttr != null) {
                                    viewAttrs.add(mSkinAttr)
                                }
                            } catch (e: NumberFormatException) {
                                e.printStackTrace()
                            }
                        }

                    }

                    if (!SkinListUtil.isEmpty(viewAttrs)) {
                        // 换肤的View，必须设置Id
                        if (viewId != 0) {
                            val skinItem = SkinItem()
                            // 获取创建的View
                            skinItem.view = rootView.findViewById(viewId)
                            skinItem.attrs = viewAttrs
                            mSkinItemMap[skinItem.view] = skinItem
                            // 判断是否为默认皮肤，不是默认皮肤时进行换肤操作
                            if (!SkinManager.isCurrentDefaultSkin()) {
                                skinItem.apply()
                            }
                        }
                    }
                }
            }
            return mSkinItemMap
        } catch (exception: Exception) {
            return mSkinItemMap;
        }
    }

    /**
     * 执行换肤
     */
    fun updateSkin(mSkinItemMap: Map<View,SkinItem>) {
        if (mSkinItemMap.isEmpty()) return

        for (view in mSkinItemMap.keys) {
            mSkinItemMap[view]?.apply()
        }
    }

}